/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.dialog

import android.content.Context
import android.graphics.PixelFormat
import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import android.widget.TextView
import com.ecarx.systemui.plugin.AppConstants
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.ui.dialog.base.AutoCloseDialog
import com.ecarx.systemui.plugin.utils.DensityUtils
import com.ecarx.systemui.plugin.utils.LogUtils

class WpcDialog : AutoCloseDialog {

    companion object {
        private const val TAG = "WpcDialog"
    }

    private var mPositionX = 0;
    private var mTvContent: TextView? = null

    constructor(pos: Int, context: Context) : this(context, 0) {
        mPositionX = pos
    }

    constructor(context: Context, themeResId: Int) : super(context, themeResId)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initConfig()
        setContentView(R.layout.dialog_wpc)
        initView()
    }

    private fun initView() {
        mTvContent = findViewById<TextView>(R.id.tv_content)
    }

    private fun initConfig() {
        val window = window
        if (window != null) {
            val params = WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                AppConstants.PLUGIN_DIALOG_TYPE,
                WindowManager.LayoutParams.FLAG_FULLSCREEN or
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSPARENT
            )
            params.gravity = Gravity.START or Gravity.TOP
            params.y = DensityUtils.dp2px(94f)
            params.x = mPositionX
            LogUtils.d(TAG, "params.x= $mPositionX")
            window.attributes = params
            setCanceledOnTouchOutside(true)
        }
    }

    fun show(status: Int) {
        super.show()
        update(status)
    }

    fun updateConfig(location: Int) {
        mPositionX = location
        initConfig()
    }

    fun update(status: Int) {
        // 状态映射
        when (status) {

            -1, 6 -> {
                mTvContent?.text = context.resources.getString(R.string.wpc_charge_failed)
            }

            4 -> {
                mTvContent?.text = context.resources.getString(R.string.wpc_charging)
            }

            5 -> {
                mTvContent?.text = context.resources.getString(R.string.wpc_charge_completed)
            }

            else -> {
                mTvContent?.text = context.resources.getString(R.string.wpc_charge_placeholder)

            }
        }
    }
}
